﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CardMonitor.Controls
{
    /// <summary>
    /// PlateKeyboard.xaml 的交互逻辑
    /// </summary>
    /// 
   
    public partial class PlateKeyboard : UserControl
    {
        public PlateKeyboard()
        {
            InitializeComponent();
            this.Visibility = Visibility.Collapsed;
            this.IsHitTestVisible = false;
        }
        public static readonly RoutedEvent KeyPressedEvent =
        EventManager.RegisterRoutedEvent("KeyPressed", RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), typeof(PlateKeyboard));

        public static readonly RoutedEvent DeletePressedEvent =
            EventManager.RegisterRoutedEvent("DeletePressed", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(PlateKeyboard));

        public static readonly RoutedEvent ClearPressedEvent =
            EventManager.RegisterRoutedEvent("ClearPressed", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(PlateKeyboard));

        public static readonly RoutedEvent ConfirmPressedEvent =
            EventManager.RegisterRoutedEvent("ConfirmPressed", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(PlateKeyboard));

        // 事件访问器
        public event RoutedEventHandler KeyPressed
        {
            add { AddHandler(KeyPressedEvent, value); }
            remove { RemoveHandler(KeyPressedEvent, value); }
        }

        public event RoutedEventHandler DeletePressed
        {
            add { AddHandler(DeletePressedEvent, value); }
            remove { RemoveHandler(DeletePressedEvent, value); }
        }

        public event RoutedEventHandler ClearPressed
        {
            add { AddHandler(ClearPressedEvent, value); }
            remove { RemoveHandler(ClearPressedEvent, value); }
        }

        public event RoutedEventHandler ConfirmPressed
        {
            add { AddHandler(ConfirmPressedEvent, value); }
            remove { RemoveHandler(ConfirmPressedEvent, value); }
        }



        // 显示键盘并定位到目标控件下方
        public void Show(UIElement targetElement)
        {
            if (targetElement == null) return;

            // 计算位置
            var rootWindow = Window.GetWindow(this);
            if (rootWindow == null) return;

            // 获取目标元素相对于窗口的位置
            Point position = targetElement.TransformToAncestor(rootWindow)
                .Transform(new Point(0, targetElement.RenderSize.Height));

            // 设置键盘位置
            this.RenderTransform = new TranslateTransform(position.X, position.Y);

            // 显示键盘
            this.Visibility = Visibility.Visible;
            this.IsHitTestVisible = true;
        }

        // 隐藏键盘
        public void Hide()
        {
            this.Visibility = Visibility.Collapsed;
            this.IsHitTestVisible = false;
        }


        // 普通按键事件
        private void KeyButton_Click(object sender, RoutedEventArgs e)
        {
           // 标记事件已处理，防止冒泡到父容器
           

            if (sender is Button button && button.Content != null)
            {
                // 触发KeyPressed事件，并传递按键内容
                RaiseEvent(new KeyPressedRoutedEventArgs(KeyPressedEvent, button.Content.ToString()));
                e.Handled = true;  // 标记事件已处理
            }
        }

        // 删除按键事件
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(DeletePressedEvent));
        }

        // 清空按键事件
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(ClearPressedEvent));
        }

        // 完成按键事件
        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(ConfirmPressedEvent));
        }

        // 关闭按钮事件
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Hide();
        }
    }

    // 自定义按键事件参数
    public class KeyPressedRoutedEventArgs : RoutedEventArgs
    {
        public string Key { get; }

        public KeyPressedRoutedEventArgs(RoutedEvent routedEvent, string key)
            : base(routedEvent)
        {
            Key = key;
        }
    }

}
